#pragma once
#include "FileLoader.h"
#include "MathFunctions.h"
#include "Rationalcompare.h"
#include <sstream>

//Returns a string with anything after and including a "." removed
string stripFiletype(string s) {
	int extAt = s.find(".");
	if (extAt != string::npos) {
		return s.substr(0,extAt);
	}
	else
		return s;
}

namespace CupidIntegrateII {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace System::IO;
	using namespace std;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

	private: System::Windows::Forms::RichTextBox^  richTextBox1;
	private: System::Windows::Forms::Button^  displayFilesButton;
	private: System::Windows::Forms::FolderBrowserDialog^  folderBrowserDialog;
	private: String^ directoryText;
	private: array<FileInfo^>^ files;
	private: System::Windows::Forms::Button^  Calculate;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::FolderBrowserDialog^  folderBrowserDialog1;
			 String^ folderName;
	private: System::Windows::Forms::CheckBox^  clearCheckBox;

	protected: 

	protected: 

	protected: 
	


	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->richTextBox1 = (gcnew System::Windows::Forms::RichTextBox());
			this->displayFilesButton = (gcnew System::Windows::Forms::Button());
			this->Calculate = (gcnew System::Windows::Forms::Button());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->folderBrowserDialog1 = (gcnew System::Windows::Forms::FolderBrowserDialog());
			this->clearCheckBox = (gcnew System::Windows::Forms::CheckBox());
			this->SuspendLayout();
			// 
			// richTextBox1
			// 
			this->richTextBox1->Location = System::Drawing::Point(12, 100);
			this->richTextBox1->Name = L"richTextBox1";
			this->richTextBox1->ReadOnly = true;
			this->richTextBox1->Size = System::Drawing::Size(431, 371);
			this->richTextBox1->TabIndex = 4;
			this->richTextBox1->Text = L"";
			this->richTextBox1->TextChanged += gcnew System::EventHandler(this, &Form1::richTextBox1_TextChanged);
			// 
			// displayFilesButton
			// 
			this->displayFilesButton->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->displayFilesButton->Location = System::Drawing::Point(12, 12);
			this->displayFilesButton->Name = L"displayFilesButton";
			this->displayFilesButton->Size = System::Drawing::Size(182, 30);
			this->displayFilesButton->TabIndex = 0;
			this->displayFilesButton->Text = L"&Select Folder";
			this->displayFilesButton->UseVisualStyleBackColor = true;
			this->displayFilesButton->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// Calculate
			// 
			this->Calculate->Enabled = false;
			this->Calculate->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->Calculate->Location = System::Drawing::Point(294, 44);
			this->Calculate->Name = L"Calculate";
			this->Calculate->Size = System::Drawing::Size(180, 30);
			this->Calculate->TabIndex = 4;
			this->Calculate->Text = L"&Calculate";
			this->Calculate->UseVisualStyleBackColor = true;
			this->Calculate->Click += gcnew System::EventHandler(this, &Form1::button1_Click_1);
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14.25F, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point, 
				static_cast<System::Byte>(0)));
			this->label1->Location = System::Drawing::Point(200, 14);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(143, 24);
			this->label1->TabIndex = 1;
			this->label1->Text = L"Dataset &Prefix:";
			this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(349, 18);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(125, 20);
			this->textBox1->TabIndex = 2;
			this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
			// 
			// clearCheckBox
			// 
			this->clearCheckBox->AutoSize = true;
			this->clearCheckBox->Location = System::Drawing::Point(149, 57);
			this->clearCheckBox->Name = L"clearCheckBox";
			this->clearCheckBox->Size = System::Drawing::Size(120, 17);
			this->clearCheckBox->TabIndex = 3;
			this->clearCheckBox->Text = L"C&lear Previous Data";
			this->clearCheckBox->UseVisualStyleBackColor = true;
			// 
			// Form1
			// 
			this->AcceptButton = this->Calculate;
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->AutoSize = true;
			this->AutoSizeMode = System::Windows::Forms::AutoSizeMode::GrowAndShrink;
			this->ClientSize = System::Drawing::Size(498, 483);
			this->Controls->Add(this->clearCheckBox);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->Calculate);
			this->Controls->Add(this->displayFilesButton);
			this->Controls->Add(this->richTextBox1);
			this->MaximizeBox = false;
			this->Name = L"Form1";
			this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
			this->Text = L"Sphere GUI";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

		// Caleb Padilla
		// This brings up the pop up box so the user can choose what folder they want.
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

 			     System::Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog();

				 if(result == System::Windows::Forms::DialogResult::OK){
					 Calculate->Enabled = true;
					 folderName = folderBrowserDialog1->SelectedPath;
					
					 DirectoryInfo^ di = gcnew DirectoryInfo(folderName);
					 files = di->GetFiles(textBox1->Text + "*", SearchOption::TopDirectoryOnly);
				 }
			 }

	private: System::Void richTextBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {

			 }

	private: System::Void listBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {

				
			 }

	private: System::Void button1_Click_1(System::Object^  sender, System::EventArgs^  e) {
				 
				 //load in sphere data from files
				 vector<vector<double>> sphereData;
				 vector<string> filesCalculated;
				 string prefix;
				 for (int i = 0; i < files->Length; ++i) {
					 string filename = string((const char *)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(folderName + "\\" + files[i]->Name).ToPointer()));
					 prefix = string((const char *)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(textBox1->Text).ToPointer()));
					 string startOfName = string((const char *)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(files[i]->Name).ToPointer())).substr(0,prefix.size());
					 if (startOfName == prefix) {
						 FileLoader f;
						 f.load(filename);
						 sphereData.push_back(f.getVector());
						 filesCalculated.push_back(string((const char *)(System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(files[i]->Name).ToPointer())));
					 }
				 }
				 
				 if (clearCheckBox->Checked)
					richTextBox1->Text = gcnew String("");
				 
				 //make calculations and output to box
				 for(int i = 0; i < sphereData.size(); i++){
					 double meanData = mean(sphereData[i]);
					 double stdevData = stdev(sphereData[i]);
					 double kurtosisData = kurtosis(sphereData[i]);
					 double skewnessData = skewness(sphereData[i]);
					 

					 string reason = Reason(kurtosisData);
					 string skewK = SkewK(skewnessData);

					 ostringstream meanos;
					 meanos << meanData;
					 ostringstream stdevos;
					 stdevos << stdevData;
					 ostringstream kurtos;
					 kurtos << kurtosisData;
					 ostringstream skewos;
					 skewos << skewnessData;

					 string meanString = meanData == DIVIDE_BY_ZERO ? "undefined" : meanos.str();
					 string stdevString = stdevData == DIVIDE_BY_ZERO ? "undefined" : stdevos.str();
					 string skewnessString = skewnessData == DIVIDE_BY_ZERO ? "undefined" : skewos.str();
					 string kurtosisString = kurtosisData == DIVIDE_BY_ZERO ? "undefined" : kurtos.str();

					 richTextBox1->Text += gcnew String(("Dataset: " + stripFiletype(filesCalculated[i].substr(prefix.size()))).c_str()) + "\n";
					 richTextBox1->Text += gcnew String(("Mean: " + meanString + "\n").c_str());
					 richTextBox1->Text += gcnew String(("Standard Deviation: " + stdevString + "\n").c_str());
					 richTextBox1->Text += gcnew String(("Skewness: " + skewnessString  + "\n").c_str());
					 richTextBox1->Text += gcnew String(("Kurtosis: " + kurtosisString + "\n").c_str());
					 richTextBox1->Text += gcnew String((skewK + "\n").c_str());
					 richTextBox1->Text += gcnew String((reason + "\n").c_str());

					 richTextBox1->Text += gcnew String("\n");
				 }

			 }

private: System::Void textBox1_TextChanged(System::Object^  sender, System::EventArgs^  e) {

		 }
private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
		 }
};
}



